// pages/login/login.js

var util = require('MD5.js')  
Page({

  /**
   * 页面的初始数据
   */
  data: {
 
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.openid);
    this.setData({
      openid: options.openid
    }); 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  userPasswordInput: function (e) {
    this.setData({
      userPassword: e.detail.value
    })
  
  },
  logIn: function () {
    console.log(this.data);
    var that=this;
    wx.request({
      url: 'https://1234679.top.com/api/member/login',
      header: { 'content-type': 'application/x-www-form-urlencoded' },  
      data: {
        mobile: this.data.userName,
        password: util.hexMD5(this.data.userPassword) 
      },
      dataType: 'json',
      method: 'POST',
      success: function (res) {
          if (res.data.error == '') {
          that.setData({
            mid: res.data.memberid
          });
          try {
            wx.setStorageSync('loginInfo', that.data)
          } catch (e) {
            console.log(e);
          }
          wx.request({
            url: 'https://1234679.top.com/api/member/openid/update',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            data: {
              openid: that.data.openid,
              memberid: that.data.mid
            },
            dataType: 'json',
            method: 'POST',
            success: function (res) {
              console.log("openid" + that.data.openid);
              console.log("mid" + that.data.mid);
              console.log("更新openid" + res);
            }
          });


          wx.navigateTo({
             url: '../index/index'
           })
        } else {
          wx.showModal({  
            title: '提示',
            content: '密码错误',
            showCancel:false
          })
        }
       },
      fail: function (res) {
        console.log(res);
        console.log('is failed')
      }
    })
  }  
})